/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.DeviceDetail;
import controller.EditDevice;
import controller.LabDetail;
import controller.ListLabs;
import entities.Device;
import entities.Lab;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class DeviceDetailBean {

    private int deviceid;
    private Device device;
    private int labid;
    private List<Lab> labs;
    private String devicename;
    private String description;

    public int getDeviceid() {
        return deviceid;
    }

    public void setDeviceid(int deviceid) {
        this.deviceid = deviceid;
    }

    public Device getDevice() {

        return detail();
    }

    public void setDevice(Device device) {
        this.device = device;
    }

    public int getLabid() {
        Lab l = detail().getLabID();
        return l.getLabID();
    }

    public void setLabid(int labid) {
        this.labid = labid;
    }

    public List<Lab> getLabs() {
        ListLabs l = new ListLabs();
        List<Lab> la = l.getLabs();
        List<Lab> ls = new ArrayList<Lab>();
        for (Lab lb : la) {
            if (lb.getStatus()) {
                ls.add(lb);
            }
        }
        return ls;
    }

    public void setLabs(List<Lab> labs) {
        this.labs = labs;
    }

    public String getDevicename() {
        return detail().getDevicename();
    }

    public void setDevicename(String devicename) {
        this.devicename = devicename;
    }

    public String getDescription() {
        return detail().getDescription();
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Creates a new instance of DeviceDetailBean
     */
    public DeviceDetailBean() {
    }

    public Device detail() {
        DeviceDetail dd = new DeviceDetail();
        return dd.deviceDetail(deviceid);
    }

    public String resultEdit() {
        Device d = detail();
        LabDetail ld = new LabDetail();
        d.setLabID(ld.labDetail(labid));
        d.setDescription(description);
        d.setDevicename(devicename);
        EditDevice ed = new EditDevice();
        boolean editDevice = ed.editDevice(d);
        if (editDevice) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Action complete !"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Action error !"));
        return "";
    }
}
